﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TrainingCenterSystem.UserManagement;
using TrainingCenterSystem.VocabularyManagement;

namespace TrainingCenterSystem.TestManagement
{
    public class TestEvaluator
    {
        #region private members
        private IndividualTest _individualTest;
        private List<GenericWord> _selectedSources;
        #endregion

        #region setters and getters
        #endregion

        #region constructors
        public TestEvaluator(IndividualTest aTest, List<GenericWord> selectedSources)
        {
            _individualTest = aTest;
            _selectedSources = selectedSources;
            this.runEvaluation();
        }
        #endregion

        #region methods
        private void runEvaluation()
        {
            for (int i = 0; i < _individualTest.TestQuestions.Count; i++)
            {
                if (_individualTest.TestQuestions[i] is FreeTextQuestion)
                {
                    this.runSimpleFreeTextEvaluation(_individualTest.TestQuestions[i], i);
                }                
            }
        }

        private void runSimpleFreeTextEvaluation(TestQuestion aQuestion, int index)
        {
            // check if the given answer is correct
            FreeTextQuestion freetext=(FreeTextQuestion)aQuestion;            
            if (_individualTest.TestAnswers[index]!=null)
            {
                if (_individualTest.TestAnswers[index].AnswerText==freetext.ExpectedAnswer)
                    _individualTest.TestAnswers[index].AnswerStatus=AnswerStatus.Correct;
                else
                {
                    this.runAdvancedFreeTextEvaluation(index);
                    if (_individualTest.TestAnswers[index].AnswerStatus == AnswerStatus.Unknown)
                        _individualTest.TestAnswers[index].AnswerStatus = AnswerStatus.Incorrect;
                }
            }
            else
                _individualTest.TestAnswers[index].AnswerStatus=AnswerStatus.Incorrect;
        }

        private void runAdvancedFreeTextEvaluation(int index)
        {            
            string actualAnswer = _individualTest.TestAnswers[index].AnswerText;
            string questionWord=_individualTest.TestQuestions[index].WordString;
            for (int i = 0; i < _selectedSources.Count; i++)
            {
                if ((_selectedSources[i].Translations == questionWord) &&
                    (_selectedSources[i].BaseValue == actualAnswer))
                {
                    _individualTest.TestAnswers[index].AnswerStatus = AnswerStatus.Partial;
                }
                else if ((_selectedSources[i].BaseValue == questionWord) &&
                    (_selectedSources[i].Translations == actualAnswer))
                {
                    _individualTest.TestAnswers[index].AnswerStatus = AnswerStatus.Partial;
                }
            }
        }
        #endregion

    }
}
